package com.cx.base.day26_序列化;

import java.io.*;

/**
 * 反序列化
 */
public class UnserializableDemo {

    public static void main(String[] args) {

        File file = new File("d:\\file\\serializable_demo\\file2.txt");
//        if(file.isFile() && !file.exists()){
//            try {
//                file.createNewFile();
//            } catch (IOException e) {
//                System.out.println("文件创建失败"+e.getMessage());
//            }
//        }
        try {
            if(!file.exists()){
                throw new IOException("文件路径为空");
            }
        } catch (IOException e) {
            System.out.println("文件创建失败："+e.getMessage());
        }
        FileInputStream fis = null;
        try{
            fis = new FileInputStream(file);
            ObjectInputStream ois = null;
            try{
                ois = new ObjectInputStream(fis);
                Object obj = ois.readObject();
                if(obj instanceof Pepole){
                    Pepole pepole = (Pepole) obj;
                    System.out.println("读取信息："+pepole);
                }
            }catch (IOException e){
                try{
                    ois.close();
                }catch (IOException e2){
                    System.out.println("ois关闭失败"+e2.getMessage());
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }catch (IOException e){
            try{
                fis.close();
            }catch (IOException e4){
                System.out.println("fis关闭失败"+e4.getMessage());
            }
        }

    }
}
